package my.merborn.ordersysteam105.controllers;

import my.merborn.ordersysteam105.models.dao.OrderMapper;
import my.merborn.ordersysteam105.models.pojo.OrderT;
import my.merborn.ordersysteam105.models.services.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class PayProcessController {

    @Autowired
    OrderService os;

    @Autowired
    OrderMapper om;

    @RequestMapping("/paypage.html")
    public ModelAndView payPage(HttpServletRequest request,@RequestParam(required = true) String uid){
        //String uid = (String) request.getSession().getAttribute("uid");
        //List<OrderT> userOrderList = om.selectByUid(Integer.parseInt(uid));
        System.out.println("uid =" +  uid);
        List<OrderT> userOrderList = om.selectNoPayed(Integer.parseInt(uid));
        ModelAndView mv = new ModelAndView("paypage.html");
        mv.addObject("userOrderList",userOrderList);
        return mv;
    }
    @RequestMapping("/payit.do")
    public ModelAndView payIt(@RequestParam(required = true) String oid){
        OrderT order = om.selectByPrimaryKey(Integer.parseInt(oid));
        double v = os.payIt(order);
       // System.out.println("payStatus is "+ v); output is " payStatus is 1.0"
        ModelAndView mv = new ModelAndView();
            mv.setViewName("redirect:paypage.html?uid="+order.getuId());
        
        return mv;
    }
}
